﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinyMoocSite.Filters;
using TinyMoocSite.Helpers;
using TinyMoocSite.Models;
using TinyMoocSite.ViewModels;

namespace TinyMoocSite.Controllers
{
    [Authorize]
    [HeaderFooterFilter]
    public class AdministratorController : Controller
    {
        [HeaderFooterFilter]
        public ActionResult Index()
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            CourseList courseList = helper.GetCourseList();
            return View(courseList);
        }
                
        public ActionResult Account()
        {
            AccountOverview overview = new AccountOverview();
            UserInfoHelper helper = new UserInfoHelper();
            helper.ListUserInfo(overview);
            return View(overview);
        }
        
        public ActionResult UserInfo(string Username)
        {
            UserDetailForm userDetail = new UserDetailForm();
            UserInfoHelper helper = new UserInfoHelper();
            UserInfo userInfo = helper.GetUserInfoByUsername(Username);
            if (userInfo != null)
            {
                userDetail.Email = userInfo.Email;
                userDetail.MyFullName = userInfo.FullName;
                userDetail.MyLicenseId = userInfo.LicenseId;
                userDetail.Username = userInfo.Username;
                userDetail.LastActivityDate = userInfo.LastActivityDate.ToLongDateString();
                userDetail.Memo = userInfo.Memo;
                userDetail.Privilege = userInfo.Privilege.ToString();
                userDetail.ProfTitle = userInfo.ProfTitle;
                userDetail.RegisterDate = userInfo.RegisterDate.ToLongDateString();
                userDetail.Website = userInfo.Website;

                return View("UserInfo", userDetail);
            }
            else
            {
                return RedirectToAction("Index");
            }                
        }
        
        public ActionResult ResetPassword(string Username)
        {
            return RedirectToAction("/Account");
        }
        
        public ActionResult CreateTeacher()
        {
            NewTeacherForm bvm = new NewTeacherForm();
            return View(bvm);
        }

        [HttpPost]
        public ActionResult DoCreateTeacher(NewTeacherForm register)
        {
            UserInfoHelper helper = new UserInfoHelper();
            if (register.Password == register.ConfirmedPassword &&
                helper.GetUserInfoByUsername(register.Username) == null)
            {
                helper.InsertUserInfo(register);
                return RedirectToAction("/Account");
            }
            return RedirectToAction("/CreateTeacher");
        }
        
        public ActionResult CreateCourse()
        {
            CourseBasicInfoForm bvm = new CourseBasicInfoForm();
            return View(bvm);
        }

        [HttpPost]
        public ActionResult DoCreateCourse(CourseBasicInfoForm register)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.InsertCourseInfo(register);            
            return RedirectToAction("/");
        }
        
        public ActionResult UpdateCourse(string courseId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            CourseBasicInfoForm bvm = helper.GetCourseInfo(courseId);
            if (bvm == null)
                return RedirectToAction("Index");
            return View(bvm);
        }

        [HttpPost]
        public ActionResult DoUpdateCourse(CourseBasicInfoForm register)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.UpdateCourseInfo(register, Request["TeacherList"].Split(','));
            return RedirectToAction("/");
        }
    }
}